<?php
    namespace app\index\controller;
    use app\index\model\Profile;
    use app\index\model\Book;
    use app\index\model\User as UserModel;
    use think\Controller;
    class User2
    {
        // 关联新增数据
        public function add()
        {
            $user = new UserModel;
            $user->name = 'thinkphp';
            $user->password = '123456';
            $user->nickname = '流年';
            if($user->save())
            {
                // 写入关联数据
                $profile = new Profile;
                $profile->truename = '刘晨';
                $profile->birthday = '1977-03-05';
                $profile->address = '中国上海';
                $profile->email = 'thinkphp@qq.com';
                $user->profile()->save($profile);
                return '用户新增成功';
            } else {
                return $user->getError();
            }
        }

        // 关联查询
        public function read($id)
        {
            $user = UserModel::get($id,'profile');
            echo '**用户信息输出*********************' . '<br>';
            echo '** '.$user->nickname . '<br>';
            echo '** '.$user->name . '<br>';
            echo '** '.$user->create_time . '<br>';
            echo '** '.$user->status . '<br>';
            echo '** '.$user->profile->truename . '<br>';
            echo '** '.$user->profile->email . '<br>';
            echo '**********************************' . '<br>';
        }

        // 一对一的关联更新
        public function update($id)
        {
            $user = UserModel::get($id);
            $user->name = 'framework';
            if($user->save())
            {
                // 更新关联数据
                $user->profile->email = 'liu2lst@qq.com';
                $user->profile->save();
                return '用户[' . $user->name . ']更新成功';
            } else {
                return $user->getError();
            }
        }

        // 关联删除
        public function delete($id)
        {
            $user = UserModel::get($id);
            if($user->delete())
            {
                // 删除关联数据
                $user->profile->delete();
                return '用户[' . $user->name . ']删除成功';
            } else {
                return $user->getError();
            }
        }

        // 关联新增图书
        public function addBook($uid)
        {
            $user = UserModel::get($uid);
            $book = new Book;
            $book->title = 'ThinkPHP5快速入门';
            $book->publish_time = '2016-05-06';
            $user->books()->save($book);
            return '为用户[' . $user->name . ']图书添加成功';
        }

        // 一对多关联,批量增加数据
        public function addBooks($uid)
        {
            $user = UserModel::get($uid);
            $book = new Book;
            $books = [
                ['title'=>'ThinkPHP5开发手册','publish_time'=>'2016-03-06'],
                ['title'=>'学通PHP的24堂课','publish_time'=>'2011-07-02'],
            ];
            $user->books()->saveAll($books);
            return '为用户['.$user->name.']批量添加图书成功';
        }

        // 根据提供的用户id进行关联查询
        public function readByuid($uid)
        {
            $user = UserModel::get($uid,'books');
            $books = $user->books()->getByTitle('ThinkPHP5快速入门');
            dump($books);
        }

        // 删除部分关联图书数据
        public function deleteBook($uid)
        {
            $user = UserModel::get($uid);
            if($user)
            {
                $book = $user->books()->getByTitle('ThinkPHP5开发手册');
                $book->delete();
                return '用户['.$user->name.']的《ThinkPHP5开发手册》图书删除成功';
            }else{
                $user->getError();
            }
        }

        // 删除全部关联的图书数据
        public function deleteBooks($uid)
        {
            $user = UserModel::get($uid);
            if($user){
                $user->books()->delete();
                return '用户[' . $user->name . ']的所有图书删除成功';
            }else{
                $user->getError();
            }
        }

        // 多对多关联新增
        public function add3($id)
        {
            $user = UserModel::get($id);
            // 新增用户角色，并自动写入枢纽表
            $user->roles()->save(['name'=>'editor','title'=>'编辑']);
            return '用户['.$user->name.']角色新增成功';
        }



    }
?>